// rmq_ext.cpp: 定义 DLL 应用程序的导出函数。
//

#include "ali_rmq.h"
#include <string.h>
#include <iostream>
#include <sys/time.h>

using namespace ons;

//const char* ONSFactoryProperty::LogPath;
//const char* ONSFactoryProperty::ProducerId;
//const char* ONSFactoryProperty::ConsumerId;
//const char* ONSFactoryProperty::PublishTopics;
//const char* ONSFactoryProperty::MsgContent;
//const char* ONSFactoryProperty::ONSAddr;
//const char* ONSFactoryProperty::AccessKey;
//const char* ONSFactoryProperty::SecretKey;
//const char* ONSFactoryProperty::MessageModel;
//const char* ONSFactoryProperty::BROADCASTING;
//const char* ONSFactoryProperty::CLUSTERING;
//const char* ONSFactoryProperty::SendMsgTimeoutMillis;
//const char* ONSFactoryProperty::NAMESRV_ADDR;
//const char* ONSFactoryProperty::ConsumeThreadNums;
//const char* ONSFactoryProperty::OnsChannel;
//const char* ONSFactoryProperty::MaxMsgCacheSize;
//const char* ONSFactoryProperty::OnsTraceSwitch;
//const char* ONSFactoryProperty::SendMsgRetryTimes;

DLL_EXPORT_C_DECL void* WINAPI ProducerStart(char* pid, char* key, char* secret)
{//producer创建和正常工作的参数，必须输入
	ONSFactoryProperty factoryInfo;
	factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, pid);//在ONS控制台申请的producerId
	factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, key);//ONS AccessKey
	factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, secret);// ONS SecretKey
	factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics, "");// 在ONS 控制台申请的msg topic
	factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "test");
	factoryInfo.setFactoryProperty(ONSFactoryProperty::LogPath, "log");
	//factoryInfo.setFactoryProperty(ONSFactoryProperty::ONSAddr, "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet")

	//创建producer
	Producer *pProducer = ONSFactory::getInstance()->createProducer(factoryInfo);
	//在发送消息前，必须调用start方法来启动Producer，只需调用一次即可。
	pProducer->start();
	return pProducer;
}

DLL_EXPORT_C_DECL void WINAPI ProducerShutdown(void* pProducer)
{
	// 在应用退出前，必须销毁Producer对象，否则会导致内存泄露等问题
	((Producer *)pProducer)->shutdown();
}

DLL_EXPORT_C_DECL void* WINAPI ProducerSendMsg(void* pProducer, char* topic, char* tag, char* key, char* content, int deliverTimeMs=0)
{
	Message msg(
		//Message Topic
        topic,
		//Message Tag,可理解为Gmail中的标签，对消息进行再归类，方便Consumer指定过滤条件在ONS服务器过滤        
		tag,
		//Message Body，任何二进制形式的数据，ONS不做任何干预，需要Producer与Consumer协商好一致的序列化和反序列化方式
        content
	);

	// 设置代表消息的业务关键属性，请尽可能全局唯一。
	// 以方便您在无法正常收到消息情况下，可通过ONS Console查询消息并补发。
	// 注意：不设置也不会影响消息正常收发
	msg.setKey(key); //"ORDERID_100");

    if(deliverTimeMs > 0)
        msg.setStartDeliverTime(deliverTimeMs);

	//发送消息，只要不抛异常就是成功
	char* rtn = new char[1024];
	try
	{
        SendResultONS sendResult = ((Producer *)pProducer)->send(msg);
        strcpy(rtn, sendResult.getMessageId());
	}
	catch (ONSClientException &e)
	{
		//添加对exception的处理
		strcpy(rtn, e.GetMsg());
//        rtn = e.GetError();
	}
	return rtn;
}


DLL_EXPORT_C_DECL void* WINAPI OrderProducerStart(char* pid, char* key, char* secret)
{//producer创建和正常工作的参数，必须输入
	ONSFactoryProperty factoryInfo;
	factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, pid);//在ONS控制台申请的producerId
	factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, key);//ONS AccessKey
	factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, secret);// ONS SecretKey
	factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics, "");// 在ONS 控制台申请的msg topic
	factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "test");
	factoryInfo.setFactoryProperty(ONSFactoryProperty::LogPath, "log");
	//factoryInfo.setFactoryProperty(ONSFactoryProperty::ONSAddr, "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet")

	//创建producer
	OrderProducer *pProducer = ONSFactory::getInstance()->createOrderProducer(factoryInfo);
	//在发送消息前，必须调用start方法来启动Producer，只需调用一次即可。
	pProducer->start();
	return pProducer;
}

DLL_EXPORT_C_DECL void WINAPI OrderProducerShutdown(void* pProducer)
{
	// 在应用退出前，必须销毁Producer对象，否则会导致内存泄露等问题
	((OrderProducer *)pProducer)->shutdown();
}

DLL_EXPORT_C_DECL void* WINAPI OrderProducerSendMsg(void* pProducer, char* topic, char* tag, char* key, char* content, char* sharding_key)
{
	Message msg(
		//Message Topic
        topic,
		//Message Tag,可理解为Gmail中的标签，对消息进行再归类，方便Consumer指定过滤条件在ONS服务器过滤
		tag,
		//Message Body，任何二进制形式的数据，ONS不做任何干预，需要Producer与Consumer协商好一致的序列化和反序列化方式
        content
	);

	// 设置代表消息的业务关键属性，请尽可能全局唯一。
	// 以方便您在无法正常收到消息情况下，可通过ONS Console查询消息并补发。
	// 注意：不设置也不会影响消息正常收发
	msg.setKey(key); //"ORDERID_100");

	//发送消息，只要不抛异常就是成功
	char* rtn = new char[1024];
	try
	{
        SendResultONS sendResult = ((OrderProducer *)pProducer)->send(msg, sharding_key);
        strcpy(rtn, sendResult.getMessageId());
	}
	catch (ONSClientException &e)
	{
		//添加对exception的处理
		strcpy(rtn, e.GetMsg());
//        rtn = e.GetError();
	}

	return rtn;
}

